package com.zhuoyue.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.zhuoyue.common.utils.StringUtils;
import com.zhuoyue.system.domain.TblProductInfo;
import com.zhuoyue.system.domain.dto.InfraredDeviceRepDTO;
import com.zhuoyue.system.domain.dto.InfraredDeviceReqDTO;
import com.zhuoyue.system.mapper.SysDeptMapper;
import com.zhuoyue.system.mapper.ProductInfoMapper;
import com.zhuoyue.web.utils.OrgCodeUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhuoyue.common.annotation.Log;
import com.zhuoyue.common.core.controller.BaseController;
import com.zhuoyue.common.core.domain.AjaxResult;
import com.zhuoyue.common.enums.BusinessType;
import com.zhuoyue.system.domain.InfraredDevice;
import com.zhuoyue.system.service.IInfraredDeviceService;
import com.zhuoyue.common.utils.poi.ExcelUtil;
import com.zhuoyue.common.core.page.TableDataInfo;

/**
 * 红外设备Controller
 *
 * @author zyzn
 * @date 2021-09-09
 */
@RestController
@RequestMapping("/system/infrared/device")
public class InfraredDeviceController extends BaseController {
    @Autowired
    private IInfraredDeviceService infraredDeviceService;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private OrgCodeUtils orgCodeUtils;

    @Autowired
    private ProductInfoMapper sysImageMapper;

    /**
     * 查询红外设备列表
     */
    @GetMapping("/list")
    public TableDataInfo list(InfraredDeviceReqDTO infraredDeviceDto) {
        startPage();
        InfraredDevice infraredDevice = new InfraredDevice().dtoToDo(infraredDeviceDto);
        List<InfraredDevice> list = infraredDeviceService.selectInfraredDeviceList(infraredDevice);
        List<InfraredDeviceRepDTO> infraredDeviceReps = new ArrayList<>(10);
        list.forEach(l -> {
            if (StringUtils.isNotEmpty(l.getZigbeeDeviceType())) {
                TblProductInfo tblProductInfo = sysImageMapper.selectProdDefaultName(StringUtils.substring(l.getZigbeeDeviceType(), 0, 8));
                l.setZigbeeDeviceDefaultName(tblProductInfo.getProductName());
            }
            InfraredDeviceRepDTO infraredDeviceRep = new InfraredDeviceRepDTO().doToDto(l);
            infraredDeviceReps.add(infraredDeviceRep);
        });
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setRows(infraredDeviceReps);
        return dataTable;
    }

    /**
     * 导出红外设备列表
     */
    @Log(title = "红外设备", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InfraredDevice infraredDevice) {
        List<InfraredDevice> list = infraredDeviceService.selectInfraredDeviceList(infraredDevice);
        List<InfraredDeviceRepDTO> infraredDeviceReps = new ArrayList<>(50);
        list.forEach(l -> {
            InfraredDeviceRepDTO infraredDeviceRep = new InfraredDeviceRepDTO().doToDto(l);
            infraredDeviceReps.add(infraredDeviceRep);
        });
        ExcelUtil<InfraredDeviceRepDTO> util = new ExcelUtil<InfraredDeviceRepDTO>(InfraredDeviceRepDTO.class);
        return util.exportExcel(infraredDeviceReps, "红外设备");
    }

    /**
     * 获取红外设备详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(infraredDeviceService.selectInfraredDeviceById(id));
    }

    /**
     * 新增红外设备
     */
    @Log(title = "红外设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InfraredDevice infraredDevice) {
        return toAjax(infraredDeviceService.insertInfraredDevice(infraredDevice));
    }

    /**
     * 修改红外设备
     */
    @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "红外设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfraredDevice infraredDevice) {
        return toAjax(infraredDeviceService.updateInfraredDevice(infraredDevice));
    }

    /**
     * 删除红外设备
     */
    @Log(title = "红外设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(infraredDeviceService.deleteInfraredDeviceByIds(ids));
    }
}
